<template>
  <div id="createCourse">
    <!-- 就诊时间选择 -->
    <div class='mc' v-show='isTime' @click.stop='toggle(0)'></div>
    <div class='datetime'>
      <div class='top'>
        <a @click.stop='toggle(0)'>取消</a>
        <a @click.stop='sureTime'>确定</a>
      </div>
      <mt-picker :slots="slotsTime" @change="onValuesChangeTime"></mt-picker>
    </div>
    <header>
      <i @click="goBack()"></i>
      <div>创建新课程</div>
    </header>
    <div class="swiper-container swipe" id="outSwiper">
      <div class="swiper-wrapper">
        <div class="swiper-slide">
          <ul class='single'>
            <li class='no-border'>
              <a>
                <label>选择直播类型:</label>
                <span class='red'>保存后不可更改</span>
              </a>
              <a class="liveType">
                <label @click="hide1()">
                  <input type="radio" v-model="issingleFree" value='free'>
                  <i>免费</i>
                </label>
                <label @click="show1()">
                  <input type="radio" v-model="issingleFree" value='charge'>
                  <i>收费</i>
                </label>
              </a>
              <a class="liveType">
                <input type="text" placeholder="输入金额" v-model='singlePrice' style="width:1.1rem" v-if="show">
              </a>
            </li>
            <li>
              <a>
                <label>直播主题:</label>
                <input type="text" placeholder="请输入直播主题" v-model='singleLiveTheme' />
              </a>
            </li>
            <li @click='toggle(1)'>
              <a>
                <label>开始时间:</label>
                <div class='time'>{{pickerValue || "请选择开播时间"}}</div>
              </a>
            </li>
            <li class='no-border'>
              <a class="descTitle">
                <label>课程介绍:</label>
              </a>
            </li>
            <li class="">
              <a class="courseDesc">
                <textarea placeholder="请输入课程介绍" v-model='singleCourseDesc'></textarea>
              </a>
            </li>
            <li class='no-border'>
              <a class="uploadImg">
                <div class="img" v-if="isShow">
                  <img class="selectImg" :src="singleUrl1" width="50" height="50" />
                </div>
                <div class="upfileDiv">
                  <el-upload class="avatar-uploader" :action='$api.baseURLUpload+"/photos/upload_shop"' :show-file-list="false"
                    :on-success="(res,file)=>{
					                    return handleAvatarSuccess(res,file,1)
					                  }"
                    :before-upload="beforeAvatarUpload">
                    <img v-if="!singleUrl1" :src="singleUrl1" class="avatar">
                    <i v-else class="uploader">上传课程照片</i>
                  </el-upload>
                </div>

              </a>
            </li>
            <div class="border-8"></div>
            <li class='no-border'>
              <a class="descTitle">
                <label>老师介绍:</label>
              </a>
            </li>
            <li class="">
              <a class="courseDesc">
                <textarea placeholder="请填写个人介绍" v-model='v_desc'></textarea>
              </a>
            </li>
            <li class='no-border'>
              <a class="uploadImg">
                <div class="img" v-if="isShow">
                  <img class="selectImg" :src="singleUr" width="50" height="50" />
                </div>
                <div class="upfileDiv">
                  <el-upload class="avatar-uploader" :action='$api.baseURLUpload+"/photos/upload_shop"' :show-file-list="false"
                    :on-success="(res,file)=>{
                                              return handleAvatarSuccess1(res,file,1)
                                            }"
                    :before-upload="beforeAvatarUpload">
                    <img v-if="!singleUrl1" :src="singleUr" class="avatar">
                    <i v-else class="uploader">上传个人照片</i>
                  </el-upload>
                </div>

              </a>
            </li>
            <li>
              <a @click='singleNext' class="nextStep">
                完 成
              </a>
            </li>
          </ul>
        </div>
      </div>
    </div>

  </div>
</template>
<script>
  import {
    Indicator,
    Toast
  } from 'mint-ui';
  export default {
    data() {
      return {
        show: false, //价钱
        isShow: true, //选择图片是否显示(个人)
        issingleFree: 'free', //免费free 收费charge
        singlePrice: '', //收费金额
        singleLiveTheme: '', //直播主题
        valuestime: '', //开播时间选择但未确定时的选项
        pickerValue: '', //开播时间  时间选择器
        isTime: false, //是否显示时间选择器
        slotsTime: [{
            flex: 1,
            values: function () {
              const date = new Date();
              const year = date.getFullYear();
              const yearArr = [];
              for (var i = year; i < year + 10; i++) {
                yearArr.push(i);
              }
              return yearArr;
            }(),
            className: 'slot1',
            textAlign: 'center'
          },
          {
            flex: 1,
            values: function () {
              const monthArr = [];
              for (var i = 1; i < 13; i++) {
                if (i < 10) {
                  monthArr.push('0' + i);
                } else {
                  monthArr.push(i);
                }

              }
              return monthArr;
            }(),
            className: 'slot2',
            textAlign: 'center'
          },
          {
            flex: 1,
            values: function () {
              const dayArr = [];
              for (var i = 1; i < 32; i++) {
                if (i < 10) {
                  dayArr.push('0' + i);
                } else {
                  dayArr.push(i);
                }

              }
              return dayArr;
            }(),
            className: 'slot3',
            textAlign: 'center'
          },
          {
            flex: 1,
            values: function () {
              const hourArr = [];
              for (var i = 0; i < 24; i++) {
                if (i < 10) {
                  hourArr.push('0' + i);
                } else {
                  hourArr.push(i);
                }

              }
              return hourArr;
            }(),
            className: 'slot4',
            textAlign: 'center'
          },
          {
            flex: 1,
            values: function () {
              const minArr = [];
              for (var i = 0; i < 60; i++) {
                if (i < 10) {
                  minArr.push('0' + i);
                } else {
                  minArr.push(i);
                }
              }
              return minArr;
            }(),
            className: 'slot5',
            textAlign: 'center'
          }
        ],
        singleCourseDesc: '', //课程介绍内容
        singleUrl1: require('../../image/bg_pic.png'), //封面
        //老师介绍
        v_desc: '', //个人描述
        singleUr: require('../../image/bg_pic.png'), //封面
        isFree: 'free', //系列课程  免费free 收费charge
        cid: "", //新创建课程id
      }
    },
    components: {

    },
    methods: {
      hide1() {
        this.show = false;
      },
      show1() {
        this.show = true;
      },

      // 控制日期选择器显示隐藏动画
      toggle(status) { //status 0隐藏 1显示
        if (status) {
          this.isTime = true;
          $(".datetime").addClass("up").removeClass("down");
        } else {
          $(".datetime").addClass("down").removeClass("up");
          this.isTime = false;
        }
      },
      //开播时间
      onValuesChangeTime(picker, values) {
        this.valuestime = values[0] + '-' + values[1] + '-' + values[2] + ' ' + values[3] + ':' + values[4];

        function mGetDate(year, month) {
          var d = new Date(year, month, 0);
          return d.getDate();
        }
        var totalDay = mGetDate(values[0], values[1]); //29 
        const dayArr = [];
        for (var i = 1; i < totalDay + 1; i++) {
          if (i < 10) {
            dayArr.push('0' + i);
          } else {
            dayArr.push(i);
          }
        }
        picker.setSlotValues(2, dayArr)
      },
      //确定开播时间
      sureTime() {
        this.pickerValue = this.valuestime;
        this.toggle(0);
      },
      //上传个人照片
      handleAvatarSuccess1(res, file, i) {
        if (res.state == 200) {
          this.singleUr = res.result_origin;
        }
      },
      //上传课程照片
      handleAvatarSuccess(res, file, i) {
        if (res.state == 200) {
          this.singleUrl1 = res.result_origin;
        }
      },
      beforeAvatarUpload(file) {
        if (!/image\/\w+/.test(file.type)) {
          this.$utils._toast('请确保文件为图像类型');
          return false;
        }
        if (file.size > 5 * 1024 * 1024) {
          this.$utils._toast('上传图片不能超过5M');
          return false;
        }
      },
      goBack() {
        this.$router.back();
      },
      //完成
      singleNext() {
        console.log(this.singleLiveTheme)
        console.log(this.pickerValue)
        console.log(this.singleCourseDesc)
        console.log(this.singleUrl1)
        console.log(this.v_desc)
        console.log(this.singleUr)
        if (this.singleLiveTheme && this.pickerValue && this.singleCourseDesc && this.singleUrl1 &&
          this.v_desc && this.singleUr) {
          const data_course = new Object(); //课程内容
          data_course.iffree = this.issingleFree;
          data_course.course_name = this.singleLiveTheme; //直播主题
          data_course.starttime = this.pickerValue; //开始时间
          data_course.program = this.singleCourseDesc; //课程介绍
          data_course.course_pic = this.singleUrl1; //课程照片
          data_course.teacher_intro = this.v_desc; //老师介绍
          data_course.teacher_pic = this.singleUr; //老师照片
          data_course.teacher_name = localStorage.getItem("doctorName");; //老师姓名
          data_course.uid = localStorage.getItem("loginMobile");
          const singleClass = {
            data_course
          };
          console.log('singleClass', singleClass)
          if (this.issingleFree == 'free') {} else {
            data_course.charge = this.singlePrice; //原价
          }
          //创建免费单节课程
          this.$api.post(this.$api.baseURLSchool, "course_video_add", singleClass, r => {
            if(r.data.ret==1){
                this.$utils._toast('创建课程成功');
            }        
          }, err => {
            console.log(err);
          })

        } else {
          this.$utils._toast('请检查上述内容是否输入完整');
        }
      },
    },
    created() {

    },
    mounted() {
    
    }
  }

</script>
<style lang="scss">
  .uploader {
    width: 0.5rem;
    height: 0.5rem;
    line-height: 0.25rem;
    font-size: 10px;
    display: inline-block;
    border: 1px dashed #eee;
  }

  #uploadFile p {
    margin-top: 10px;
  }

  #uploadFile .mesBox .message {
    background: transparent;
  }

</style>
